package design.simdi.code.final_paper.site.panels.proiecte.bibliografie;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Restrictions;

import design.simdi.code.final_paper.core.daos.BibliographyDao;
import design.simdi.code.final_paper.core.entities.Bibliography;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;

/**
 * 
 * @author Marius Dumitriu - (Jun 21, 2010 - 3:32:53 PM)
 * 
 */
public class BibliografieTabPanel extends PagePanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SpringBean
	private BibliographyDao bibliographyDao;

	public BibliografieTabPanel(String id) {
		super(id);
		InjectorHolder.getInjector().inject(this);
		((WicketApplication) getApplication()).setShowErrors(true);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void panelInit() {
		List<IColumn<Bibliography>> columns = new ArrayList<IColumn<Bibliography>>();

		columns.add(new PropertyColumn<Bibliography>(new ResourceModel("name"), "name", "name"));
		columns.add(new PropertyColumn<Bibliography>(new ResourceModel("description"), "description"));
		columns.add(new AbstractColumn<Bibliography>(new ResourceModel("link"), "link") {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item cellItem, String componentId, IModel rowModel) {
				Bibliography bibliography = (Bibliography) rowModel.getObject();
				Label label = new Label(componentId, "<a href='" + bibliography.getLink() + " '><span>"
						+ bibliography.getName() + "</span></a>");
				label.setEscapeModelStrings(false);
				add(label);

				cellItem.add(label);

			}

		});

		SortableBibliographyDataProvider sortableBibliographyDataProvider = new SortableBibliographyDataProvider();
		sortableBibliographyDataProvider.getCriterii().add(Restrictions.eq("person.id", getCurrentPerson().getId()));

		add(new MyDataTable<Bibliography>("table", columns, sortableBibliographyDataProvider, getRowsPerPage()) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public ActionToolbar getActionToolbar() {
				return new BibliographyActionToolbar(this);
			}
		});
	}

	public int getRowsPerPage() {
		return 10;
	}

	public void setBibliographyDao(BibliographyDao bibliographyDao) {
		this.bibliographyDao = bibliographyDao;
	}

	public BibliographyDao getBibliographyDao() {
		return bibliographyDao;
	}

}
